<?php
/**
 * Created by PhpStorm.
 * User: xuke
 * Date: 16/6/29
 * Time: 下午3:31
 */

namespace App\Logic;
use App\Http\Model\UserAddress As UserAddressModel;
use App\Exceptions\ApiException;

class UserAddressLogic extends BaseLogic
{
    protected $_userAddress = null;

    public function __construct($address)
    {
        $this->_userAddress = $address;
    }

    public static function initByUserAddress($userAddress)
    {
        if (is_numeric($userAddress))
        {
            $userAddress = UserAddressModel::find($userAddress);
            if (!$userAddress)
            {
                throw new ApiException('用户地址信息不存在', -2);
            }
        }else if(!$userAddress instanceof UserAddressModel)
        {
            throw new ApiException('用户地址参数错误', -2);
        }
        return new static($userAddress);
    }

    public static function create()
    {
        $userAddress = new UserAddressModel();
        return new static($userAddress);
    }

    public function getUserAddress()
    {
        return $this->_userAddress;
    }

    public function checkUser($user_id)
    {
        if ($this->getUserAddress()->user_id != $user_id)
        {
            throw new ApiException('用户地址与用户不匹配', -2);
        }
    }
}